package com.monster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.monster.dto.SetmealDTO;
import com.monster.entity.PageResult;
import com.monster.entity.QueryPageBean;
import com.monster.mapper.SetMealMapper;
import com.monster.pojo.CheckGroup;
import com.monster.pojo.CheckItem;
import com.monster.pojo.Member;
import com.monster.pojo.Setmeal;
import com.monster.service.CheckGroupService;
import com.monster.service.SetMealService;
import com.monster.utils.aliyunoss.AliyunUtils;
import com.monster.utils.redis.RedisUtil;
import com.monster.utils.resources.RedisConstant;
import com.monster.vo.CheckGroupVO;
import com.monster.vo.SetmealVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {


    @Autowired
    CheckGroupService checkGroupService;

    @Override
    public Boolean addOrUpdataCheckGroupDTO(SetmealDTO setmealDTO) {
            //若是修改CheckGroupDTO会有id
            Integer setmealDTOId = setmealDTO.getId();
            if(setmealDTOId != null){
                //修改前先删除对应的表的值
                baseMapper.deleteCheckgroupCheckitem(setmealDTOId);
                //查询之前的图的名字，判断是否与现在的相同 若不相同放入set集合
                String imageUrl = baseMapper.findImageUrl(setmealDTOId);
                if(!imageUrl.equals(setmealDTO.getImg())){
                    RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,imageUrl);
                }
            }

            //进行修改或者增加
            boolean save = this.saveOrUpdate(setmealDTO);
            Integer sid = setmealDTO.getId();
            Integer[] checkitemIds = setmealDTO.getCheckgroupIds();
            //循环添加
            if(checkitemIds != null && checkitemIds.length >0){
                for (Integer cid : checkitemIds) {
                    baseMapper.addCheckgroupSetMeal(sid,cid);
                }
            }
         RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setmealDTO.getImg());
//         RedisUtil.removeSetMember(RedisConstant.SINGLE_PIC+setmealDTO.getImg(),setmealDTO.getImg());
            return save;
        }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Setmeal> page;
        QueryWrapper<Setmeal> checkGroupQueryWrapper = new QueryWrapper<Setmeal>();
        checkGroupQueryWrapper.eq("IS_DELETE", 0);
        //无条件查寻
        if (queryPageBean.getQueryString() == null) {
            page = this.page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), checkGroupQueryWrapper);
            return new PageResult(page.getTotal(), page.getRecords());
        }
        //有条件查询
        else {
            checkGroupQueryWrapper.like("HELPCODE",queryPageBean.getQueryString());
            page = this.page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), checkGroupQueryWrapper);
            return new PageResult(page.getTotal(), page.getRecords());
        }
    }
    //
    @Override
    public SetmealVO showDataBySid(Integer id) {
            SetmealVO checkGroupVO = new SetmealVO();
            List<CheckGroup> checkGroupList = checkGroupService.list();
            checkGroupVO.setCheckGruopList(checkGroupList);
            List<Integer> findcheckgroups = baseMapper.findchegroups(id);
            checkGroupVO.setCheckGruopIdList(findcheckgroups);
            String imageUrl = baseMapper.findImageUrl(id);
            checkGroupVO.setImageUrl(imageUrl);
        return checkGroupVO;
    }

    //数据删除
    @Override
    public Boolean deleteById(int id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setIs_delete(1);
        int type = baseMapper.updateById(setmeal);
        return type != 0;
    }

    @Override
    public List<Setmeal> findAllSetmealForH5() {
        return baseMapper.findAllSetmealForH5();
    }

    @Override
    public SetmealVO findSetMealDetail(int id) {
        //通过id查询setmeal的详情
        SetmealVO setmealVOList = baseMapper.findSetmealById(id);
        //通过id查询项目组的数据
        List<CheckGroupVO> cherkGroupList = baseMapper.findCherkGroupById(id);
        for (CheckGroupVO checkGroupVO : cherkGroupList) {
            List<CheckItem> checkItemById = baseMapper.findCheckItemById(checkGroupVO.getId());
            checkGroupVO.setCheckItemList(checkItemById);
        }
        setmealVOList.setCheckGroupVOList(cherkGroupList);
        return setmealVOList;
    }

    @Override
    public Setmeal findSetMeal(int id) {
        Setmeal setmeal = baseMapper.findSetMeal(id);
        return setmeal;
    }

    @Override
    @LcnTransaction
    public Boolean updateOrderSetting(Integer setmealId) {
        return  baseMapper.updateOrderSetting(setmealId);
    }

    @Override
    public Integer isMore(String setmealId) {
        return baseMapper.isMore(setmealId);
    }


    //    清理  oss  垃圾图片   该方法 有  xxl-job 去调用
    @Override
    public void clearOssImg() {
        //获取set的文件名
        Set<String> membersOfSet = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        if(membersOfSet.size() != 0){
            for (String uuidfilename : membersOfSet) {
                //判断Rdeis中是否有文件
                boolean existsKey = RedisUtil.existsKey(RedisConstant.SINGLE_PIC+uuidfilename);
                if(existsKey == false){
                    //如果没有阿里云删除
                    AliyunUtils.deleteFile(uuidfilename);
                    System.out.println(" 阿里云删除文件 "+uuidfilename);
                    //set集合中删除
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);
                }
            }
        }
    }
}

